package com.bot.telegramvariousbot.dao;


import com.bot.telegramvariousbot.model.TgDraw;
import com.bot.telegramvariousbot.model.TgDrawActivity;
import com.bot.telegramvariousbot.model.TgDrawRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class LuckDrawDaoExt extends BaseService {
    private static Logger logger = LoggerFactory.getLogger(LuckDrawDaoExt.class);

    public int insertTgDrawRecordBean(TgDrawRecord tgDrawRecord){
        String sql = "insert tg_draw_record (tg_uid,user_name,code,note) values (:tgUid,:userName,:code,:note)";
        try{
            return new NamedParameterJdbcTemplate(jdbcTemplateW).update(sql, new BeanPropertySqlParameterSource(tgDrawRecord));
        }catch (Exception e){
            logger.error("insert insertTgDrawRecordBean is error",e);
            return -1;
        }
    }

    public TgDrawActivity getTgDrawById(int id) {
        try {
            String sql = "SELECT * FROM tg_draw_activity WHERE id =?";
            List<TgDrawActivity> list = jdbcTemplateR.query(sql, TgDrawActivity.ROWMAPPER, id);
            if (list != null && list.size() != 0) {
                return list.get(0);
            } else {
                return null;
            }
        } catch (Exception e) {
            logger.error("getTgDrawById is error", e);
            return null;
        }
    }

    public int insertTgDrawBean(TgDraw tgDraw){
        String sql = "insert tg_draw (block,block_height,block_code,open_time,draw_code,user_name,draw_tg_uid,note) values (:block,:blockHeight,:blockCode,:openTime,:drawCode,:userName,:drawTgUid,:note)";
        try{
            return new NamedParameterJdbcTemplate(jdbcTemplateW).update(sql, new BeanPropertySqlParameterSource(tgDraw));
        }catch (Exception e){
            logger.error("insert insertTgDrawBean is error",e);
            return -1;
        }
    }

    public TgDrawRecord getTgDrawRecordByTgUid(String tg_uid) {
        try {
            String sql = "SELECT * FROM tg_draw_record WHERE tg_uid =?";
            List<TgDrawRecord> list = jdbcTemplateR.query(sql, TgDrawRecord.ROWMAPPER, tg_uid);
            if (list != null && list.size() != 0) {
                return list.get(0);
            } else {
                return null;
            }
        } catch (Exception e) {
            logger.error("getTgDrawRecordByTgUid is error", e);
            return null;
        }
    }

    public TgDrawRecord getTgDrawRecordByDrawCode(long code) {
        try {
            String sql = "SELECT *,ABS(`CODE`-?) AS absnum FROM `tg_draw_record` ORDER BY absnum,create_time LIMIT 1;";
            List<TgDrawRecord> list = jdbcTemplateR.query(sql, TgDrawRecord.ROWMAPPER, code);
            if (list != null && list.size() != 0) {
                return list.get(0);
            } else {
                return null;
            }
        } catch (Exception e) {
            logger.error("getTgDrawRecordByDrawCode is error", e);
            return null;
        }
    }

    public TgDraw getTgDrawLimitOne() {
        try {
            String sql = "SELECT * FROM `tg_draw` LIMIT 1";
            List<TgDraw> list = jdbcTemplateR.query(sql, TgDraw.ROWMAPPER);
            if (list != null && list.size() != 0) {
                return list.get(0);
            } else {
                return null;
            }
        } catch (Exception e) {
            logger.error("getTgDrawLimitOne is error", e);
            return null;
        }
    }

}
